package domain.worm;

import com.uqbar.vainilla.DeltaState;

public class JumpingState extends WormState {
	
	private double speed = 0;
	private double aceleracion = 2;
	private double maxSpeed = 0.5;
	private boolean flagFall = false;


	public JumpingState(Worm worm) {
		super(worm);
		if(this.getWorm().getDir() == 1)
			this.getWorm().setAppearance(this.getWorm().getJumpRightAnimation());
		else
			this.getWorm().setAppearance(this.getWorm().getJumpLeftAnimation());

	}

	@Override
	public void update(DeltaState deltaState) {

		double vix = Math.cos(45);
		double viy = Math.sin(45);
		
		if((this.speed < this.maxSpeed ) & !flagFall){
			if(!this.getWorm().canWalk(deltaState, this.getWorm().getDir())){
				this.getWorm().setState(new StateFalling(this.getWorm()));
			}else{
				this.speed = this.speed + this.aceleracion * deltaState.getDelta();
				this.getWorm().updateCoords(this.getWorm().getDir()*vix, -(this.speed*viy));}
		}else{
			this.flagFall = true;
			this.speed = this.speed + (-this.aceleracion) * deltaState.getDelta();
			this.getWorm().updateCoords(this.getWorm().getDir()*vix, -(this.speed*viy));
			if(!this.getWorm().canWalk(deltaState, this.getWorm().getDir()))
				this.getWorm().setState(new StateFalling(this.getWorm()));
			}
	}
	
	
}

